package com.dragon.logical.service.supplies.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.model.ResultCode;
import com.dragon.common.model.exception.CustomException;
import com.dragon.logical.service.supplies.IProductService;
import com.dragon.repository.entity.Product;
import com.dragon.repository.mapper.ProductCategoryMapper;
import com.dragon.repository.mapper.ProductMapper;
import com.dragon.repository.mapper.ProductUnitMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 商品表（SPU） 服务实现类
 * </p>
 *
 * @author sy
 * @since 2023-09-11
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    @Autowired
    ProductMapper mapper;
    @Autowired
    ProductCategoryMapper categoryMapper;
    @Autowired
    ProductUnitMapper unitMapper;


    @Override
    public Page<Product> listProduct(Integer page, Integer limit, Integer categoryId , String search) {
        Page<Product> pageModel = new Page<>(page, limit);
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.lambda().like(StrUtil.isNotBlank(search), Product::getName,search)
                .eq(categoryId != null, Product::getCategoryId,categoryId);
        Page<Product> productPage = mapper.selectPage(pageModel, wrapper);
        for (Product record : productPage.getRecords()) {
            record.setCategoryName(categoryMapper.getCategoryName(record.getCategoryId()));
            record.setProductUnitName(unitMapper.getProductUnitName(record.getProductUnitId()));
            //record.setFinanceUnitName(unitMapper.getFinanceUnitName(record.getFinanceUnitId()));
        }
        return productPage;
    }


    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void saveProduct(Product product) {
        //商品SPU信息添加
        product.setCompanyId(1);
        product.setCreateTime(DateUtil.date());
        mapper.insert(product);

    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void updateProduct(Product product){
        Product product1 = mapper.selectById(product.getId());
        if(product1 == null){
            throw new CustomException(ResultCode.DATA_NOT_FOUND);
        }
        product.setUpdateTime(DateUtil.date());
        mapper.updateById(product);
    }

    @Override
    public void deleteProduct(Integer id){
        mapper.deleteById(id);
    }

    @Override
    public Product detailById(Integer id) {
        Product product = mapper.selectById(id);
        product.setCategoryName(categoryMapper.getCategoryName(product.getCategoryId()));
        product.setProductUnitName(unitMapper.getProductUnitName(product.getProductUnitId()));
        //product.setFinanceUnitName(unitMapper.getFinanceUnitName(product.getFinanceUnitId()));
        return product;
    }

}
